package com.UtilsPak;

import javax.faces.model.SelectItem;
import java.util.List;
import java.util.Iterator;

import locale.LocaleConf;

/**
 * Created by IntelliJ IDEA.
 * User: mchebanenko
 * Date: 5-Jan-2011
 * Time: 11:36:10 AM
 * To change this template use File | Settings | File Templates.
 */
public class CommonFunctions {

    public static SelectItem[] getListSelectItem(List l, String value) {

       Integer i = 0;

       Iterator itr = l.iterator() ;
       SelectItem[] retVal = new  SelectItem [l.size()];
        while(itr.hasNext() ){
            try{
                 if(value.equals("")) {
                    Object col = itr.next();
                    retVal[i] = (new SelectItem(col.toString(),col.toString()));
                } else {
                    Object[] col = (Object[])itr.next();
                    String tag;
                    if(col[2]==null){
                        tag= col[1].toString();
                    } else {
                        tag= col[2].toString();
                    }

                    retVal[i] = (new SelectItem(col[0].toString(), LocaleConf.getLocalized(tag),col[1].toString()));
                }
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                retVal=null;
                retVal[0]=(new SelectItem("",""));
                return retVal;
            }
            i++;
        }
        return retVal;
    }


    public static String ArrayToString (Object[] value) {
        String retVal = "";
        if (value != null){
            for(Object o: value){
                retVal =retVal + o.toString() + ",";
            }
        retVal=retVal.substring(0,retVal.length()-1);
        }
        return  retVal;
    }

    public static boolean isItemInArray (Object[] arr,Object val) {
        boolean retVal = false;
        if (arr != null){
            for(Object o: arr){
                 if (o.equals(val)){
                    retVal = true;
                 }
            }
        }
        return  retVal;
    }


    public static String getValueFromRef(List ref,String value) {
        String retVal = "";

             try{
                if( ref!=null){
                    retVal = ref.get(Integer.parseInt(value)).toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return  retVal;
            }

        return  retVal;
    }
}
